//
//  CellConfig.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2021/5/24.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import Foundation
import UIKit
import RxSwift

// MARK: - 动态绑定key
private struct AssociateKeys {
    
    // base collection cell propertys key
    /// 背景色
    static var bgColor: Void?
    /// 高度
    static var cellHeight: Void?
    static var cellHeightSubject: Void?
    
    /// size
    static var cellSize: Void?
    
    // long press collection cell propertys key
    // 是否支持长按
    static var isLongPress: Void?
    static var isLocationFirst: Void?
    static var isLocationLast: Void?
    
    static var isConfigDone: Void? /// 是否配置结束
    static var isCellConfigDone: Void? /// 是否配置结束
    
    static var isShow: Void? /// 是否显示
}


// MARK: - cell config
public protocol CellConfigProtocol: AssociatedObjectStore {
    
    var model: Any? { get set }
    
    init(_ model: Any?)
    
}

public extension CellConfigProtocol {
    
    // 背景色
    var bgColor: UIColor?  {
        get {
            return associatedObject(forKey: &AssociateKeys.bgColor)
        }
        set {
            setAssociatedObject(newValue, forKey: &AssociateKeys.bgColor, ploicy: .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    // cell 是否配置结束 自动执行
    var isCellConfigDone: Bool {
        get {
            return associatedObject(forKey: &AssociateKeys.isCellConfigDone, default: false, ploicy: .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        set {
            setAssociatedObject(newValue, forKey: &AssociateKeys.isCellConfigDone, ploicy: .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    // 是否配置结束 需要手动 写
    var isConfigDone: Bool {
        get {
            return associatedObject(forKey: &AssociateKeys.isConfigDone, default: false, ploicy: .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        set {
            setAssociatedObject(newValue, forKey: &AssociateKeys.isConfigDone, ploicy: .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    // 是否显示
    var isShow: Bool {
        get {
            return associatedObject(forKey: &AssociateKeys.isShow, default: true, ploicy: .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        set {
            setAssociatedObject(newValue, forKey: &AssociateKeys.isShow, ploicy: .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
}

// MARK: - table cell config
public protocol TableCellConfigProtocol: CellConfigProtocol  {
    
    func cellCls() -> UITableViewCell.Type
    
    func cellOnce(_ cell: UITableViewCell) -> Void
    func cellEvery(_ cell: UITableViewCell) -> Void
    
}

public extension TableCellConfigProtocol {
    
    /// 缓存cell高度 默认自动高度
    var cellHeight: CGFloat  {
        get {
            return associatedObject(forKey: &AssociateKeys.cellHeight, default: UITableView.automaticDimension, ploicy: .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        set {
            setAssociatedObject(newValue, forKey: &AssociateKeys.cellHeight, ploicy: .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    /// 刷新cell高度的subject
    var cellHeightSubject: BehaviorSubject<CGFloat>  {
        get {
            return associatedObject(forKey: &AssociateKeys.cellHeightSubject, default: BehaviorSubject<CGFloat>(value: 0.0), ploicy: .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        set {
            setAssociatedObject(newValue, forKey: &AssociateKeys.cellHeightSubject, ploicy: .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    /// cell 执行
    func cellOnce(_ cell: UITableViewCell) -> Void { }
    
    func cellEvery(_ cell: UITableViewCell) -> Void { }
    
    /// cell cls
    func cellCls() -> UITableViewCell.Type {
        return UITableViewCell.self
    }
    
    
}

/// 动态 添加
public protocol PublicTableCellConfigProtocol: TableCellConfigProtocol  {
    
    associatedtype TargetType

    func once(_ target: TargetType, cell: UITableViewCell)

    func every(_ target: TargetType, cell: UITableViewCell)
    
}

// MARK: - collection view 的 cell 的 config
public protocol CollectionCellConfigProtocol: CellConfigProtocol {
    
    func cellCls() -> UICollectionViewCell.Type
    
    func cellOnce(_ cell: UICollectionViewCell)
    func cellEvery(_ cell: UICollectionViewCell)
    
}

public extension CollectionCellConfigProtocol {
    
    var cellSize: CGSize  {
        get {
            return associatedObject(forKey: &AssociateKeys.cellSize, default: CGSize(0, 0), ploicy: .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        set {
            setAssociatedObject(newValue, forKey: &AssociateKeys.cellSize, ploicy: .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    /// cell 执行
    func cellOnce(_ cell: UICollectionViewCell) -> Void { }
    func cellEvery(_ cell: UICollectionViewCell) -> Void { }
    
}

/// 需要长按着的cv  的 cell
public protocol CollectionCellLongPressConfigProtocol: CollectionCellConfigProtocol { }

public extension CollectionCellLongPressConfigProtocol {
    
    var isLongPress: Bool  {
        get {
            return associatedObject(forKey: &AssociateKeys.isLongPress, default: true, ploicy: .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        set {
            setAssociatedObject(newValue, forKey: &AssociateKeys.isLongPress, ploicy: .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    var isLocationFirst: Bool  {
        get {
            return associatedObject(forKey: &AssociateKeys.isLocationFirst, default: false, ploicy: .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        set {
            setAssociatedObject(newValue, forKey: &AssociateKeys.isLocationFirst, ploicy: .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    var isLocationLast: Bool  {
        get {
            return associatedObject(forKey: &AssociateKeys.isLocationLast, default: false, ploicy: .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        set {
            setAssociatedObject(newValue, forKey: &AssociateKeys.isLocationLast, ploicy: .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
}

public protocol PublicCollectionCellConfigProtocol: CollectionCellLongPressConfigProtocol  {
    
    associatedtype TargetType

    func once(_ target: TargetType, cell: UICollectionViewCell)

    func every(_ target: TargetType, cell: UICollectionViewCell)
    
}
